
#include "systick.h"
#include "apm32f0xx.h"



volatile static uint32_t delay;




/*!
    \brief      configure systick
    \param[in]  none
    \param[out] none
    \retval     none
*/
void systick_config(void)
{
    /* setup systick timer for 1000Hz interrupts */
    if (SysTick_Config(SystemCoreClock / 1000U))
    {
        /* capture error */
        while (1)
        {
        }
    }
    /* configure the systick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x00U);
}


/*!
    \brief      delay a time in milliseconds
    \param[in]  count: count in milliseconds
    \param[out] none
    \retval     none
*/
void delay_1ms(uint32_t count)
{
    delay = count;

    while(0U != delay)
    {
    }
}

/*!
    \brief      delay decrement
    \param[in]  none
    \param[out] none
    \retval     none
*/
void delay_decrement(void)
{
    if (0U != delay)
    {
        delay--;
    }
}






/*******************************************************
*    \brief      delay_us
*   \param[in]  us
*    \param[out] none
*    \retval     none
*******************************************************/
void delay_us(uint16_t us)
{
    uint16_t i=0,j=0;
    for(i=0; i<us; i++)
    {
        for(j=0; j<1; j++)
        {
            __NOP();
            __NOP();
            __NOP();
            __NOP();
//        __NOP();  (1/48)*1.25=260ns
        }

    }
}

/*******************************************************
*\brief      delay_ms
*\param[in]    ms
*\param[out] none
*\retval     none
*******************************************************/
void delay_ms(uint16_t ms)
{
    uint16_t i=0;
    for(i=0; i<ms; i++)
    {
        delay_us(962);   //1000 000 /1040 =961.5
    }
}





























